"use strict";
define(function(require, exports, module) {
    var _public = module.exports = {};
    var $ = require('jquery'),
        common = require('lib/common'),
        template = require('lib/template'),
        report = require('business/report'),
        router = require('business/router');

      _public.users = {
            loginid: $(".js-loginid").data("id"),
            visiterid: $(".js-visitid").data("id")
        }


    _public.init = function() {

        var date = Date.now();
        console.log('/faceb-cupt/newsbefore/time/' + date + '/count/10');
        /* $.post('/faceb-cupt/newsbefore/time/'+date+'/count/10', {}, function(data, textStatus, xhr) {
            if(typeof data&&data.status==0){
               var container = $("#article-content");
               common.template("article-index-tpl", data, container);
            }else{
              console.log(data.info);
              alert("加载失败，请稍后再试！");
            }  
           
          });*/
        $.ajax({
                url: '/faceb-cupt/newsbefore/time/' + date + '/count/10',
                dataType: "json",  
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
                type: 'POST',
                data: {},
            })
            .done(function(data) {
            console.log(data);
            console.log(data.status);
                if (data.status == "0") {
                    var container = $("#article-content");
                    common.template("article-index-tpl", data, container);


                    //goto detail
                    
                     $(".js-godetail").on("click", function(e) {
                        var nid = $(this).data("nid");
                        var uid=$(this).data("uid")
                        window.location.href = "/faceb-cupt/tpl/news/detail/" +uid + "?word=" + nid;
                    });
                     

                     //go other
                     $(".js-author").on("click",function(e){
                  	  
                    //	 window.location.reload();
                  	    window.location.href="/faceb-cupt/tpl/center/users/"+$(this).data("uid");
                    	
                     });
                     
                     
                     //edit like
                     var like_btn = $(".js-elike");
                     like_btn.on("click", function(e) {
                     	
                         if(!window.addlike){;
                             window.addlike=true;
                             var nid = $(this).parents(".nidway").data("nid");
                             var btn_this=$(this);
                             $.ajax({
                                 url: '/faceb-cupt/news/like/' + nid,
                                 dataType: "json",
                                 contentType: "application/x-www-form-urlencoded; charset=utf-8",
                                 type: 'POST',
                                 data: {},
                             }).done(function(data) {
                                 console.log("like test");
                                 console.log(data);
                                 if(data&&data.status=="0"){
                                    var edit_end=btn_this.data("like")+1;
                                    btn_this.text(edit_end+"喜欢");
                                    btn_this.data("like",edit_end);
                                 }
                             });
                         }else{
                             window.addlike=true;
                         }
                         

                     });
                     

                } else {
                    console.log(data.info);
                    alert("加载失败，请稍后再试！");
                }
            })
            .fail(function(data) {
                console.log("error");
            })
            .always(function(data) {
                console.log("complete");
            });



        //bindEvent
        var header_btn = $(".js-header");
        header_btn.on("click", function(e) {
            $(".personal-box").toggleClass('hide');
        });






        var goindex=$(".js-index");
        var visitid=$(".js-visitid").data("id");
        goindex.on("click",function(e){
            window.location.href='/faceb-cupt/tpl/center/users/'+visitid;
        });

        var gocenter=$(".js-center");
        gocenter.on("click",function(e){
            var loginid=$(".js-loginid").data("id");
            window.location.href="/faceb-cupt/tpl/center/users/"+loginid;

        });

    };
});
